package fi.piet.ec2.employee;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.link.PageLink;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.spring.injection.annot.SpringBean;

import fi.piet.ec2.HomePage;
import fi.piet.ec2.department.Department;
import fi.piet.ec2.service.DepartmentService;
import fi.piet.ec2.service.EmployeeService;

public class EmployeePage extends WebPage {

	@SpringBean
	private EmployeeService employeeService;

	@SpringBean
	private DepartmentService departmentService;

	public EmployeePage(final PageParameters parameters) {

		List<Employee> empList = new ArrayList<Employee>(employeeService
				.getAllEmployees());

		final Collection<Department> depList = departmentService.getAllDepartments();

		Employee newEmp = new Employee();
		add(new EmployeeForm("addEmployeeForm", newEmp, depList));

		add(new ListView("employeeList", empList) {
			/**
			 * 
			 */
			private static final long serialVersionUID = 6900644892131835325L;

			@SuppressWarnings("serial")
			public void populateItem(final ListItem item) {
				final Employee emp = (Employee) item.getModelObject();
				item.add(new Label("id", "" + emp.getId()));
				item.add(new Label("firstname", emp.getFirstname()));
				item.add(new Label("lastname", emp.getLastname()));

				Link edit = new Link("edit") {
					@Override
					public void onClick() {
						EditEmployee ep = new EditEmployee(emp, depList);
						setResponsePage(ep);
					}
				};
				item.add(edit);
			}
		});

		add(new PageLink("homePageLink", HomePage.class));
	}

}
